﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "custom/area/effect" {
    Properties {
        _Color ("Tint", Color) = (1,1,1,1)
        _AreaHalfWidth("Area Half Width", Float) = 5
        _Speed("Speed", Float) = 0.5
        _RangeA("RangeA", Float) = 0.3
        _RangeB("RangeB", Float) = 0.7
    }
    SubShader {
		
		Tags
		{
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}
        
        Pass {
			Cull Off
			Lighting Off
			ZWrite Off
			Fog { Mode Off }
			Blend SrcAlpha OneMinusSrcAlpha
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			fixed4 _Color;
			float _AreaHalfWidth;
			float _Speed;
			float _RangeA;
			float _RangeB;
			
			struct appdata_t
			{
				float4 vertex   : POSITION;
				float4 color    : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 vertex   : SV_POSITION;
				fixed4 color    : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			v2f vert (appdata_t v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos (v.vertex);
				o.texcoord = v.texcoord;
				o.color = v.color * _Color;
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
//				float offset2 = sqrt(pow(i.texcoord.x, 2) + pow(i.texcoord.y, 2));
				float offset2 = max(abs(i.texcoord.x), abs(i.texcoord.y));
//				float a = clamp(offset2 / (_AreaHalfWidth * 1.5), 0, 1);
//				float p = 2 * (1 - a) + 1 * a;
//				float offset = pow(pow(abs(i.texcoord.x + i.texcoord.y), p) + pow(abs(i.texcoord.x - i.texcoord.y), p), 1 / p);
				float offset = offset2;
				
				offset = fmod(fmod((offset + _Speed * _Time.y) / _AreaHalfWidth, 1) + 1, 1);
				float rb = 1 - _RangeB;
				float alpha = max(1 - clamp(offset, 0, _RangeA) / _RangeA, (clamp(offset, rb, 1) - rb) / ( 1 - rb));
				
				fixed4 texcol = fixed4(i.color.rgb, i.color.a * alpha);
				return texcol;
			}
			ENDCG

        }
    }
}